function JsonCompact(){

	this.isResult      = false;
	this.ispostData    = false;
	this.factionName    = "";
	this.fmethodName    = "";
	this.fserviceCode    = "";
	this.flogID         = "";
	/**
	 * 后台返回数据结果类型：
	 * 0:后台操作;1:后台数据集;2:后台返回自行处理数据
	 */
	this.resultType      = "0";
	this.dataValue       = "";
	
	this.stringBuf     = new Yacon.util.StringBuffer();
	
}
JsonCompact.prototype.setActionName = function(actionName)
{
	this.factionName  = actionName;
}
JsonCompact.prototype.setMethodName = function(methodName)
{
	this.fmethodName  = methodName;
}
JsonCompact.prototype.setLogID = function(logID)
{
	this.flogID  = logID;
}
JsonCompact.prototype.setServiceCode = function(serviceCode)
{
	this.fserviceCode  = serviceCode;
}
/**
 * 打包数据集公共属性
 * @param dataset 数据集
 * @return 根节点
 */
JsonCompact.prototype.getDataSet = function(dataset)
{
	if (Yacon.isEmpty(this.factionName))
		this.factionName = window.className;
	
	this.stringBuf.clear();
	if (window.parameter)
	{
		this.stringBuf.append("sessionID="+window.parameter["sessionID"]+"&");
	}
	this.stringBuf.append("switch_json={'action':[{'actionName':'"+this.factionName.encode()+"','pagePath':'"+window.pagePath.encode()+"','methodName':'"+this.fmethodName.encode()+"',");
	this.stringBuf.append("'serviceCode':'"+this.fserviceCode.encode()+"','traceID':'"+new Date().getTime()+"',");

	this.stringBuf.append("'dataset':[{'attr':{");
	
	var isforeign = false;	
	if (window.parameter)
	{
		for(var name in window.parameter)
		{
			if (!name.isEqual("logid"))
				dataset.setForeignField(name,window.parameter[name]);
		}
	}
	if (dataset.foreignRecord)
	{
		for (var j=0; j< dataset.foreignRecord.getFieldCount(); j++)
		{
			field = dataset.foreignRecord.getField(j);
			if (j>0)
				this.stringBuf.append(",");
			
			this.stringBuf.append("'"+field.getFieldName()+"':'"+field.getFieldValue().encode()+"'");
			
			isforeign = true;
		}		
	}
	if (dataset.attributes)
	{
		for (var j=0; j< dataset.attributes.getFieldCount(); j++)
		{
			field = dataset.attributes.getField(j);	
			if (dataset.foreignRecord && dataset.foreignRecord.isExitField(field.getFieldName()))
			{}else
			{
				if (isforeign)
					this.stringBuf.append(",");
				else if (j>0)
					this.stringBuf.append(",");
					
				this.stringBuf.append("'"+field.getFieldName()+"':'"+field.getFieldValue().encode()+"'");
			}
		}			
	}
	this.stringBuf.append("},");
}

/**
 * 打包数据集后台操作数据据
 * @param dataset 数据集
 * @return 打包后数据
 */
JsonCompact.prototype.getPost = function(dataset)
{
	 this.ispostData = false;
	 var isforeign = false;
	 
	 this.getDataSet(dataset);	 
	 this.stringBuf.append("'record':[");
	 
	 var record = dataset.firstElement;
	 while (record)
	 {
		 if (!record.getState().isEqual("past"))
		 {
			if (this.ispostData)
				this.stringBuf.append(",");
			
			if (record.getState().isEqual("insert"))
				 this.stringBuf.append("{'type':'1',");
			if (record.getState().isEqual("edit"))
				 this.stringBuf.append("{'type':'2',");
			if (record.getState().isEqual("delete"))
				 this.stringBuf.append("{'type':'3',");
			 
			this.stringBuf.append("'field':[");    			
			
			var fieldNode,field;
			if (dataset.foreignRecord)
			{
				for (var j=0; j< dataset.foreignRecord.getFieldCount(); j++)
				{
					field = dataset.foreignRecord.getField(j);
					
					if (j>0)
						this.stringBuf.append(",");
					
					this.stringBuf.append("{'name':'"+field.getFieldName()+"','value':'"+field.getFieldValue().encode()+"','oldvalue':''}");
					
					isforeign = true;
				}		
			}
			
			for (var j=0; j< record.getFieldCount(); j++)
			{
				field = record.getField(j);	
				if (dataset.foreignRecord && dataset.foreignRecord.isExitField(field.getFieldName()))
				{}else
				{
					if (isforeign)
						this.stringBuf.append(",");
					else if (j>0)
						this.stringBuf.append(",");
					
					this.stringBuf.append("{'name':'"+field.getFieldName()+"','value':'"+field.getFieldValue().encode()+"','oldvalue':''}");
				}
			}
			this.ispostData = true;
			
			this.stringBuf.append("]}");  
		}
		record = record.nextRecord();
	}
	this.stringBuf.append("]}]}]}"); 
	return this.stringBuf.toString();
}
/**
 * 前后台数据交换
 * @param dataset 数据集
 * @return 打包数据
 */
JsonCompact.prototype.getSwitch = function(dataset)
{
	var isforeign = false; 
	this.getDataSet(dataset);	 
	this.stringBuf.append("'record':[");
	 
	 if (dataset.findRecord ||dataset.foreignRecord)
	 {
		this.stringBuf.append("{'type':'0',");
		this.stringBuf.append("'field':[");    		
		var record = dataset.foreignRecord;	
		var fieldNode,field;
		if (record)
		{
			for (var j=0; j< record.getFieldCount(); j++)
			{
				field = record.getField(j);	
				if (j>0)
					this.stringBuf.append(",");
				
				this.stringBuf.append("{'name':'"+field.getFieldName()+"','value':'"+field.getFieldValue().encode()+"','oldvalue':''}");
				
				isforeign = true;
			}		
		}
		var record = dataset.findRecord;
		if (record)
		{
			for (var j=0; j< record.getFieldCount(); j++)
			{
				field = record.getField(j);	
				if (dataset.foreignRecord && dataset.foreignRecord.isExitField(field.getFieldName()))
				{}else
				{
					if (isforeign)
						this.stringBuf.append(",");
					else if (j>0)
						this.stringBuf.append(",");
					
					this.stringBuf.append("{'name':'"+field.getFieldName()+"','value':'"+field.getFieldValue().encode()+"','oldvalue':''}");
				}
			}
		}
		this.stringBuf.append("]}"); 
	}
	 this.stringBuf.append("]}]}]}"); 
	
	 return this.stringBuf.toStr();
} 

/**
 * 获取已打包数据
 * @return
 */
JsonCompact.prototype.getData=function()
{
	return this.stringBuf.toStr();
}

/**
 * 校验从后台接收得到的数据
 * @param xmlStr 所接收数据
 * @return
 */
JsonCompact.prototype.analyzeData = function(jsonStr)
{
	
	if (window.log.allControl&&window.log.xmlRecv)
	{
		alert("JsonCompact:analyzeData:"+jsonStr);
	}
	
	this.isResult = false;
	this.isDataResult = false;
	if(Yacon.isEmpty(jsonStr)||jsonStr.isEqual("false"))
	{
		return false;
	}
  /* try{
		var obj = eval(jsonStr);
		if (obj.attr)
			this.fAttributes.jsonList = obj.attr;
		
		if (obj.fields)
		{
			for(var i=0;i<obj.fields.length;i++)
			{
				this.createRecord();
				this.activeElement.jsonList = obj.fields[i];
			}
		}
		this.beforeFirst();
	}catch(e)
	{}*/
	
	var jsonObj = eval(jsonStr);
	if (!jsonObj.action)
	{
		alert("从后台没有获取到业务数据！");
		return false;
	}
	
	this.dataValue = "";
	if (jsonObj.action.type.isEqual("1"))
	{
		this.resultType  ="1";
		if (jsonObj.action.result.isEqual("0000"))
		{
			this.dataValue = jsonObj.action.dataset;
			this.isResult = true;
			return true;
		}
		else
		{
			alert(jsonObj.action.message);
			this.isResult = false;
			return false;
		}
	}else if (jsonObj.action.type.isEqual("0000"))
	{
		this.resultType  ="0";
		if (jsonObj.action.result.isTrue())
		{
			if (!Yacon.isEmpty(jsonObj.action.message))
				alert(jsonObj.action.message);
			this.isResult = true;
			
			return true;
		}
		else
		{
			if (!Yacon.isEmpty(jsonObj.action.message))
				alert(jsonObj.action.message);
			this.isResult = false;
			return false;
		}
	}else if (jsonObj.action.type.isEqual("2"))
	{
		
		this.resultType  ="2";
		this.dataValue = jsonObj.action.message;
		this.isResult = true;
		return true;
	}	
}

/**
 * 初始化数据集
 * @param dataset 需要初始化的数据集
 * @return
 */
JsonCompact.prototype.loadDataSet= function(dataset)
{
	if (!dataset)
		return;
	
	dataset.free();
	
	if (Yacon.isEmpty(this.dataValue))
		return false;
	/**
	 * 数据返回类型
	 */
	dataset.resultType = this.resultType;
	
	if (this.resultType =="1")
	{
		this._initDataSet(this.dataValue,dataset);
	}else
	{
		
		dataset.dataValue  = this.dataValue;
	}
	
	return true;
}


/**
 * 填充数据集
 * @param dataset 需要填充的数据集
 * @return
 */
JsonCompact.prototype.fillDataSet= function(dataset)
{
	if (!Yacon.isObject(dataset))
		return;
	
	dataset.clearRecord();
	
	if (Yacon.isEmpty(this.dataValue))
		return false;
	/**
	 * 数据返回类型
	 */
	dataset.resultType = this.resultType;
	
	if (this.resultType =="1")
	{
		this._initDataSet(this.dataValue,dataset);
	}else
	{
		
		dataset.dataValue  = this.dataValue;
	}
	
	return true;
}

/**
 * 填充数据集
 * @param dataset 需要填充的数据集
 * @return
 */
JsonCompact.prototype._initDataSet= function(jsonData,dataset)
{
	if (jsonData.attr)
	{
		dataset.createAttribute();
		dataset.attributes.setJsonList(jsonData.attr);
	}
	
	/**
	 * 添加记录
	 */
	var recordSNode =  jsonData.records;
	var recordNode;	
	for (var i=0; i< recordSNode.length; i++)
	{
		recordNode = recordSNode[i];
		
		dataset.createRecord();
		dataset.setState("past");			
		dataset.activeElement.setJsonList(recordNode.fields)
		
	}
}

